﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DrugsShopManagement.DBCLASS;

using System.Runtime.InteropServices;
using System.IO;


namespace DrugsShopManagement
{
    public partial class FrmMain : DevExpress.XtraEditors.XtraForm
    {
        Operator op = new Operator();
        public string u_id, u_name, u_no,u_power;
        public FrmMain(string user_id,string user_name,string user_power)
        {
            InitializeComponent();
            u_id = user_id;
            u_name = user_name;
            u_power = user_power;
        }

        /// <summary>
        /// 报警提醒
        /// </summary>
        public void jgtx()
        {
            DataTable dt_a = new DataTable();
            string sq_ccid = "Select * from drugsStore";//库存表药品ID
            dt_a = op.getDataTable(sq_ccid, "drugsStore ");
            int n; //库存表行数

            DataTable dt_b = new DataTable();
            string sq_wid = "Select * from drugsWarning";//报警值表药品ID
            dt_b = op.getDataTable(sq_wid, "drugsWarning ");
            int m;//报警值表行数

            for (n = 0; n < dt_a.Rows.Count; n++)
            {
                for (m = 0; m < dt_b.Rows.Count; m++)
                {
                    int Strccid = Convert.ToInt32(dt_a.Rows[n][1]);//库存表药品ID
                    int strccno = Convert.ToInt32(dt_a.Rows[n][16]);//库存表药品库存量
                    int strwid = Convert.ToInt32(dt_b.Rows[m][0]);//报警值表药品ID
                    int strvno = Convert.ToInt32(dt_b.Rows[m][6]);//报警值表药品下限量

                    if (Strccid == strwid)
                    {
                        if (strccno < strvno)
                        {
                            labelTX.Visible = true ;
                        }
                        
                    }
                }
            }
        }

        private FrmUsers frmUsers;
        private FrmChangePwd frmChangePwd;
        private FrmPowerManage frmPowerManage;
        private FrmReportDesign frmReportDesign;
        private FrmDrugsInfo frmDrugsInfo;
        private FrmDrugsProvider frmDrugsProvider;
        private FrmDrugsBuy frmDrugsBuy;
        private FrmDrugsSales frmDrugsSales;
        private FrmDrugsIn frmDrugsIn;
        private FrmDrugsOut frmDrugsOut;
        private FrmDrugsBuySearch frmDrugsBuySearch;
        private XtraFrmStatistics xtraFrmStatistics;
        private FrmDrugsWarning frmDrugsWarning;
        private RibbonFrmSkin ribbonFrmSkin;
        private FrmBackup frmBackup;
        private FrmDrugsStoreManage frmDrugsStoreManage;
        private FrmDrugsInStatistics frmDrugsInStatistics;
        private FrmDrugsOutStatistics frmDrugsOutStatistics;
        private FrmDrugsSalesStatistics frmDrugsSalesStatistics;

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("您确定要退出药店药品管理系统吗?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void 用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmUsers==null||frmUsers.IsDisposed)
            {
                frmUsers=new FrmUsers();
                frmUsers.MdiParent = this;
                frmUsers.Show();
            }
            if (frmUsers!= null)
            {
                frmUsers.Activate();
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            jgtx();
            this.WindowState = FormWindowState.Maximized;
            if (u_power == "普通管理员")
            {
                this.备份还原ToolStripMenuItem.Visible = false;//普通管理员没有备份还原权限，系统管理员才有
                this.报表设计ToolStripMenuItem.Visible = false;//普通管理员没有报表设计权限，系统管理员才有
            }
            if (u_power == "库存管理员")
            {
                this.备份还原ToolStripMenuItem.Visible = false;
                this.报表设计ToolStripMenuItem.Visible = false;
                this.用户管理ToolStripMenuItem.Visible = false;
                this.采购管理ToolStripMenuItem.Visible = false;
                this.销售管理ToolStripMenuItem.Visible = false;
                //this.汇总统计ToolStripMenuItem.Visible = false;
                this.系统设置ToolStripMenuItem.Visible = false;
            }
            if (u_power == "采购员")
            {
                this.备份还原ToolStripMenuItem.Visible = false;
                this.报表设计ToolStripMenuItem.Visible = false;
                this.用户管理ToolStripMenuItem.Visible = false;
                this.库存管理ToolStripMenuItem.Visible = false;
                this.销售管理ToolStripMenuItem.Visible = false;
                //this.汇总统计ToolStripMenuItem.Visible = false;
                this.系统设置ToolStripMenuItem.Visible = false;
            }
            if (u_power == "销售员")
            {
                this.备份还原ToolStripMenuItem.Visible = false;
                this.报表设计ToolStripMenuItem.Visible = false;
                this.用户管理ToolStripMenuItem.Visible = false;
                this.采购管理ToolStripMenuItem.Visible = false;
                this.库存管理ToolStripMenuItem.Visible = false;
                //this.汇总统计ToolStripMenuItem.Visible = false;
                this.系统设置ToolStripMenuItem.Visible = false;
            }
            //记录系统登录时间
            this.toolStripStatusLabel2.Text = DateTime.Now.ToString();  

            //记录当前时间，Enable Timer1控件
            this.timer1.Interval = 100;
            this.timer1.Enabled = true;
            this.toolStripStatusLabel5.Text = DateTime.Now.ToString();
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
                frmChangePwd = new FrmChangePwd();
                frmChangePwd.ShowDialog();
        }

        private void 用户切换ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ////切换用户使得关闭系统的时候有点小问题，需改进
            this.Hide();
            FrmLogin frmLogin = new FrmLogin();
            frmLogin.Show();
        }

        private void 锁定系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmLogin frmLogin = new FrmLogin();
            frmLogin.Show();
            return;

            //if (user_pwd.Text = "")
            //{ 
            //    BlockInput(false);
            //    Application.Exit();
            //}
            //else
            //{
            //    MessageBox.show("密码错误！","温馨提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            //    user_pwd.Text="";
            //    user_pwd.Foucus();
            //}
        }

        private void 权限管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmPowerManage==null||frmPowerManage.IsDisposed)
            {
                frmPowerManage = new FrmPowerManage();
                frmPowerManage.MdiParent = this;
                frmPowerManage.Show();
            }
            if (frmPowerManage!=null)
            {
                frmPowerManage.Activate();
            }
        }

        private void 报表设计ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmReportDesign == null || frmReportDesign.IsDisposed)
            {
                frmReportDesign = new FrmReportDesign();
                frmReportDesign.MdiParent = this;
                frmReportDesign.Show();
            }
            if (frmReportDesign!=null)
            {
                frmReportDesign.Activate();
            }
        }

        private void 供应商管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsProvider == null || frmDrugsProvider.IsDisposed)
            {
                frmDrugsProvider = new FrmDrugsProvider();
                frmDrugsProvider.MdiParent = this;
                frmDrugsProvider.Show();
            }
            if (frmDrugsProvider != null)
            {
                frmDrugsProvider.Activate();
            }
        }

        private void 药品入库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsIn == null || frmDrugsIn.IsDisposed)
            {
                frmDrugsIn = new FrmDrugsIn(u_id, u_name);
                frmDrugsIn.MdiParent = this;
                frmDrugsIn.Show();
            }
            if (frmDrugsIn!=null)
            {
                frmDrugsIn.Activate();
            }
        }

        private void 药品出库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsOut == null || frmDrugsOut.IsDisposed)
            {
                frmDrugsOut = new FrmDrugsOut(u_id,u_name);
                frmDrugsOut.MdiParent = this;
                frmDrugsOut.Show();
            }
            if (frmDrugsOut != null)
            {
                frmDrugsOut.Activate();
            }
        }

        private void 警报设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsWarning == null || frmDrugsWarning.IsDisposed)
            {
                frmDrugsWarning = new FrmDrugsWarning();
                frmDrugsWarning.MdiParent = this;
                frmDrugsWarning.Show();
            }
            if (frmDrugsWarning != null)
            {
                frmDrugsWarning.Activate();
            }

        }

        private void 库存管理ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsStoreManage == null || frmDrugsStoreManage.IsDisposed)
            {
                frmDrugsStoreManage = new FrmDrugsStoreManage();
                frmDrugsStoreManage.MdiParent = this;
                frmDrugsStoreManage.Show();
            }
            if (frmDrugsStoreManage != null)
            {
                frmDrugsStoreManage.Activate();
            }
        }

        private void 药品采购ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsBuy == null || frmDrugsBuy.IsDisposed)
            {
                frmDrugsBuy = new FrmDrugsBuy(u_id, u_name);
                frmDrugsBuy.MdiParent = this;
                frmDrugsBuy.Show();
            }
            if (frmDrugsBuy!=null)
            {
                frmDrugsBuy.Activate();
            }
        }

        private void 采购查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsBuySearch == null || frmDrugsBuySearch.IsDisposed)
            {
                frmDrugsBuySearch = new FrmDrugsBuySearch();
                frmDrugsBuySearch.MdiParent = this;
                frmDrugsBuySearch.Show();
            }
            if (frmDrugsBuySearch != null)
            {
                frmDrugsBuySearch.Activate();
            }
        }

        private void 药品销售ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsSales == null || frmDrugsSales.IsDisposed)
            {
                frmDrugsSales = new FrmDrugsSales(u_no,u_name);
                frmDrugsSales.MdiParent = this;
                frmDrugsSales.Show();
            }
            if (frmDrugsSales != null)
            {
                frmDrugsSales.Activate();
            }
        }

        private void 入库统计ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsInStatistics == null || frmDrugsInStatistics.IsDisposed)
            {
                frmDrugsInStatistics = new FrmDrugsInStatistics();
                frmDrugsInStatistics.MdiParent = this;
                frmDrugsInStatistics.Show();
            }
            if (frmDrugsInStatistics != null)
            {
                frmDrugsInStatistics.Activate();
            }
        }

        private void 出库统计ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsOutStatistics == null || frmDrugsOutStatistics.IsDisposed)
            {
                frmDrugsOutStatistics = new FrmDrugsOutStatistics();
                frmDrugsOutStatistics.MdiParent = this;
                frmDrugsOutStatistics.Show();
            }
            if (frmDrugsOutStatistics != null)
            {
                frmDrugsOutStatistics.Activate();
            }

        }

        private void 销售统计ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsSalesStatistics == null || frmDrugsSalesStatistics.IsDisposed)
            {
                frmDrugsSalesStatistics = new FrmDrugsSalesStatistics();
                frmDrugsSalesStatistics.MdiParent = this;
                frmDrugsSalesStatistics.Show();
            }
            if (frmDrugsSalesStatistics != null)
            {
                frmDrugsSalesStatistics.Activate();
            }
        }

        private void 汇总统计分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (xtraFrmStatistics == null || xtraFrmStatistics.IsDisposed)
            {
                xtraFrmStatistics = new XtraFrmStatistics();
                xtraFrmStatistics.MdiParent = this;
                xtraFrmStatistics.Show();
            }
            if (xtraFrmStatistics != null)
            {
                xtraFrmStatistics.Activate();
            }

        }

        private void 备份还原ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmBackup == null || frmBackup.IsDisposed)
            {
                frmBackup = new FrmBackup();
                frmBackup.MdiParent = this;
                frmBackup.Show();
            }
            if (frmBackup != null)
            {
                frmBackup.Activate();
            }
        }

        private void 更换皮肤ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ribbonFrmSkin == null || ribbonFrmSkin.IsDisposed)
            {
                ribbonFrmSkin = new RibbonFrmSkin();
                ribbonFrmSkin.MdiParent = this;
                ribbonFrmSkin.Show();
            }
            if (ribbonFrmSkin != null)
            {
                ribbonFrmSkin.Activate();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel5.Text = DateTime.Now.ToString();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point p = new Point();
                p.X = e.Location.X + this.Location.X + 10;
                p.Y = e.Location.Y + this.Location.Y + 30;
                this.contextMenuStrip1.Show(p);
            }
        }

        private void 药品采购ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsBuy == null || frmDrugsBuy.IsDisposed)
            {
                frmDrugsBuy = new FrmDrugsBuy(u_id, u_name);
                frmDrugsBuy.MdiParent = this;
                frmDrugsBuy.Show();
            }
            if (frmDrugsBuy != null)
            {
                frmDrugsBuy.Activate();
            }
        }

        private void 药品销售ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsSales == null || frmDrugsSales.IsDisposed)
            {
                frmDrugsSales = new FrmDrugsSales(u_no,u_name);
                frmDrugsSales.MdiParent = this;
                frmDrugsSales.Show();
            }
            if (frmDrugsSales != null)
            {
                frmDrugsSales.Activate();
            }
        }

        private void 药品入库ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsIn == null || frmDrugsIn.IsDisposed)
            {
                frmDrugsIn = new FrmDrugsIn(u_id, u_name);
                frmDrugsIn.MdiParent = this;
                frmDrugsIn.Show();
            }
            if (frmDrugsIn != null)
            {
                frmDrugsIn.Activate();
            }
        }

        private void 药品出库ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (frmDrugsOut == null || frmDrugsOut.IsDisposed)
            {
                frmDrugsOut = new FrmDrugsOut(u_id, u_name);
                frmDrugsOut.MdiParent = this;
                frmDrugsOut.Show();
            }
            if (frmDrugsOut != null)
            {
                frmDrugsOut.Activate();
            }
        }

        private void 计算器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void 关于系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAbout fa = new FrmAbout();
            fa.ShowDialog();
            fa.Dispose();
        }

        private void 退出系统ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 药品信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmDrugsInfo == null || frmDrugsInfo.IsDisposed)
            {
                frmDrugsInfo = new FrmDrugsInfo();
                frmDrugsInfo.MdiParent = this;
                frmDrugsInfo.Show();
            }
            if (frmDrugsInfo != null)
            {
                frmDrugsInfo.Activate();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            FormWarn fw = new FormWarn();
            fw.Show();
        }

        private void button_Lock_Click(object sender, EventArgs e)
        {
            FrmLock frmLock = new FrmLock(u_id);
            frmLock.ShowDialog();//弹出系统锁定界面，锁定系统直到输入正确的解锁密码
        }

       
       
    }
}
